package org.jeecg.modules.flower.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.flower.entity.*;
import org.jeecg.modules.flower.exception.FlowerLoseNumException;
import org.jeecg.modules.flower.mapper.FlowerLoseMapper;
import org.jeecg.modules.flower.mapper.FlowerTagInfoMapper;
import org.jeecg.modules.flower.mapper.FlowerTagpreMapper;
import org.jeecg.modules.flower.service.IFlowerLoseInfoService;
import org.jeecg.modules.flower.service.IFlowerLoseService;
import org.jeecg.modules.flower.service.IFlowerTagInfoService;
import org.jeecg.modules.flower.service.IFlowerTagpreService;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @Description: 蔬菜货损
 * @Author: jeecg-boot
 * @Date:   2022-06-29
 * @Version: V1.0
 */
@Service
public class FlowerLoseServiceImpl extends ServiceImpl<FlowerLoseMapper, FlowerLose> implements IFlowerLoseService {

    @Resource
    private FlowerTagInfoMapper flowerTagInfoMapper;

    @Resource
    private FlowerTagpreMapper flowerTagpreMapper;

    @Resource
    private IFlowerTagpreService flowerTagPreService;

    @Resource
    private IFlowerTagInfoService flowerTagInfoService;

    @Resource
    private IFlowerLoseInfoService flowerLoseInfoService;

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public synchronized void customSave(FlowerLose flowerLose) throws Exception {

        checkValid(flowerLose);
        //设置ID
        String id = UUIDGenerator.generate();
        flowerLose.setId(id);
        this.baseMapper.insert(flowerLose);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void customEdit(FlowerLose flowerLose) throws Exception {
        checkValid(flowerLose);
        this.baseMapper.updateById(flowerLose);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public boolean loseData(FlowerLose flowerLose) throws Exception {
        checkValid(flowerLose);
        String loseType = flowerLose.getLoseType();
        Integer loseNum = flowerLose.getLoseNum();
        // 查询处理库存数据
        if("库存货损".equals(loseType)){
            // 源库库损，查源库
            Integer count = flowerTagInfoService.flowerLose(flowerLose);
            if(!count.equals(loseNum)){
                throw new FlowerLoseNumException("库存汇总符合条件数为"+count+"，请确认库存信息是否正确");
            }
        }
        if("预售货损".equals(loseType)){
            //更新预售货存状态
            Integer preSaleCount = flowerTagPreService.flowerLose(flowerLose);
            if(!preSaleCount.equals(loseNum)){
                throw new Exception("库存报损"+loseNum+"，预售库存报损"+preSaleCount+"数量不一致，请核实");
            }
        }
        flowerLose.setState(1);
        this.updateById(flowerLose);
        return true;
    }


    private boolean checkValid(FlowerLose flowerLose) throws FlowerLoseNumException{
        String loseType = flowerLose.getLoseType();
        Integer loseNum = flowerLose.getLoseNum();
        // 判断是否仓库货损
        if("库存货损".equals(loseType)){
            // 源库库损，查源库
            Integer count = flowerTagInfoMapper.checkLoseValid(flowerLose);
            if(count < loseNum){
                throw new FlowerLoseNumException("库存汇总符合条件数为"+count+"，请确认库存信息是否正确");
            }
        }

        // 判断是否预售货损
        if("预售货损".equals(loseType)){
            // 检查预出库库存
            Integer preSaleCount = flowerTagpreMapper.checkLoseValid(flowerLose);
            if(preSaleCount<loseNum){
                throw new FlowerLoseNumException("库存汇总符合条件数为"+preSaleCount+"，预售库存符合条件数有"+preSaleCount+"数量不一致，请核实");
            }
        }
        return true;
    }
}
